import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';

///图片管理
class ImageHelper {
  static const String baseUrl = '';
  static const String imagePrefix = '';

  ///网络图片
  static String wrapUrl(String url) {
    if (url.startsWith('http')) {
      return url;
    } else {}
    return imagePrefix + url;
  }

  ///本地图片
  static String wrapAssets(String url) {
    return GetPlatform.isWeb ? "/images/$url" : 'assets/images/$url';
  }

  ///加载图片插件
  static ImageProvider imgProvider(String url) {
    if (url.startsWith('http')) {
      return NetworkImage(wrapUrl(url));
    }
    return AssetImage(wrapAssets(url));
  }

  ///图片未加载完时
  static Widget placeHolder({required double width, required double height}) {
    return SizedBox(width: width, height: height, child: CupertinoActivityIndicator(radius: min(10.0, width / 3)));
  }

  ///图片加载错误
  static Widget error({double? width, double? height, double? size}) {
    return width != null
        ? SizedBox(
            width: width,
            height: height,
            child: Icon(
              Icons.error_outline,
              color: Colors.grey[300],
              size: size ?? 85,
            ),
          )
        : AspectRatio(
            aspectRatio: 1,
            child: Icon(
              Icons.error_outline,
              size: size ?? 85,
            ),
          );
  }
}
